home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / vidmgr13.zip / VMGREMX.C < prev    next >
C/C++ Source or Header  |  1996-10-03  |  17KB  |  745 lines

  1. /*
  2.  *  VMGREMX.C; VidMgr module for the EMX GNU C/C++ compiler.  Release 1.2.
  3.  *
  4.  *  This module written in April 1996 by Andrew Clarke and released to the
  5.  *  public domain.  Last modified in June 1996.
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <dos.h>
  11. #include <sys/video.h>
  12.  
  13. #define INCL_KBD
  14. #define INCL_VIO
  15. #define INCL_DOSPROCESS
  16.  
  17. #include <os2.h>
  18.  
  19. #include "vidmgr.h"
  20. #include "opsys.h"
  21.  
  22. static int vm_iscolorcard(void);
  23. static void vm_setcursorsize(char start, char end);
  24. static void vm_getcursorsize(char *start, char *end);
  25. static void vm_getkey(unsigned char *chScan, unsigned char *chChar);
  26.  
  27. static int video_init = 0;
  28.  
  29. #define vi_init()  { if (!video_init) video_init = v_init(); }
  30. #define vi_done()  { video_init = 0; }
  31.  
  32. void vm_init(void)
  33. {
  34.     vi_init();
  35.     vm_getinfo(&vm_startup);
  36.     vm_setattr(vm_startup.attr);
  37.     if (_osmode == DOS_MODE)
  38.     {
  39.         opsysDetect();
  40.     }
  41. }
  42.  
  43. void vm_done(void)
  44. {
  45.     vm_setcursorsize(vm_startup.cur_start, vm_startup.cur_end);
  46.     vi_done();
  47. }
  48.  
  49. void vm_getinfo(struct vm_info *v)
  50. {
  51.     v->ypos = vm_wherey();
  52.     v->xpos = vm_wherex();
  53.     v->attr = vm_getattrxy(v->xpos, v->ypos);
  54.     v->height = vm_getscreenheight();
  55.     v->width = vm_getscreenwidth();
  56.     vm_getcursorsize(&v->cur_start, &v->cur_end);
  57. }
  58.  
  59. char vm_getscreenwidth(void)
  60. {
  61.     if (_osmode == DOS_MODE)
  62.     {
  63.         int width, height;
  64.         vi_init();
  65.         v_dimen(&width, &height);
  66.         return (char)width;
  67.     }
  68.     else
  69.     {
  70.         VIOMODEINFO vi;
  71.         vi.cb = sizeof(VIOMODEINFO);
  72.         VioGetMode(&vi, 0);
  73.         return vi.col;
  74.     }
  75. }
  76.  
  77. char vm_getscreenheight(void)
  78. {
  79.     if (_osmode == DOS_MODE)
  80.     {
  81.         int width, height;
  82.         vi_init();
  83.         v_dimen(&width, &height);
  84.         return (char)height;
  85.     }
  86.     else
  87.     {
  88.         VIOMODEINFO vi;
  89.         vi.cb = sizeof(VIOMODEINFO);
  90.         VioGetMode(&vi, 0);
  91.         return vi.row;
  92.     }
  93. }
  94.  
  95. short vm_getscreensize(void)
  96. {
  97.     return (short)(vm_getscreenwidth() * vm_getscreenheight() * 2);
  98. }
  99.  
  100. char vm_wherex(void)
  101. {
  102.     if (_osmode == DOS_MODE)
  103.     {
  104.         int row, col;
  105.         vi_init();
  106.         v_getxy(&col, &row);
  107.         return (char)(col + 1);
  108.     }
  109.     else
  110.     {
  111.         USHORT row, col;
  112.         VioGetCurPos(&row, &col, 0);
  113.         return (char)(col + 1);
  114.     }
  115. }
  116.  
  117. char vm_wherey(void)
  118. {
  119.     if (_osmode == DOS_MODE)
  120.     {
  121.         int row, col;
  122.         vi_init();
  123.         v_getxy(&col, &row);
  124.         return (char)(row + 1);
  125.     }
  126.     else
  127.     {
  128.         USHORT row, col;
  129.         VioGetCurPos(&row, &col, 0);
  130.         return (char)(row + 1);
  131.     }
  132. }
  133.  
  134. static int vm_iscolorcard(void)
  135. {
  136.     int hw = v_hardware();
  137.     return hw == V_COLOR_8 || hw == V_COLOR_12 ? 1 : 0;
  138. }
  139.  
  140. void vm_gotoxy(char x, char y)
  141. {
  142.     if (_osmode == DOS_MODE)
  143.     {
  144.         vi_init();
  145.         v_gotoxy((int)(x - 1), (int)(y - 1));
  146.     }
  147.     else
  148.     {
  149.         VioSetCurPos((USHORT) (y - 1), (USHORT) (x - 1), 0);
  150.     }
  151. }
  152.  
  153. static void vm_setcursorsize(char start, char end)
  154. {
  155.     if (_osmode == DOS_MODE)
  156.     {
  157.         vi_init();
  158.         v_ctype(start, end);
  159.     }
  160.     else
  161.     {
  162.         VIOCURSORINFO vi;
  163.         vi.yStart = start;
  164.         vi.cEnd = end;
  165.         vi.cx = 0;
  166.         vi.attr = 0;
  167.         VioSetCurType(&vi, 0);
  168.     }
  169. }
  170.  
  171. static void vm_getcursorsize(char *start, char *end)
  172. {
  173.     if (_osmode == DOS_MODE)
  174.     {
  175.         int cstart, cend;
  176.         vi_init();
  177.         v_getctype(&cstart, &cend);
  178.         *start = (char)cstart;
  179.         *end = (char)cend;
  180.     }
  181.     else
  182.     {
  183.         VIOCURSORINFO vi;
  184.         VioGetCurType(&vi, 0);
  185.         *start = vi.yStart;
  186.         *end = vi.cEnd;
  187.     }
  188. }
  189.  
  190. static void vm_getkey(unsigned char *chScan, unsigned char *chChar)
  191. {
  192.     union REGS regs;
  193.     regs.h.ah = 0x00;
  194.     _int86(0x16, ®s, ®s);
  195.     *chScan = regs.h.ah;
  196.     *chChar = regs.h.al;
  197. }
  198.  
  199. int vm_kbhit(void)
  200. {
  201.     if (_osmode == DOS_MODE)
  202.     {
  203.         union REGS regs;
  204.         static unsigned short counter = 0;
  205.         if (counter % 10 == 0)
  206.         {
  207.             opsysTimeSlice();
  208.         }
  209.         counter++;
  210.         regs.h.ah = 0x01;
  211.         _int86(0x16, ®s, ®s);
  212.         return !(regs.x.flags & 0x40);
  213.     }
  214.     else
  215.     {
  216.         KBDKEYINFO ki;
  217.         ki.fbStatus = 0;
  218.         KbdPeek(&ki, 0);
  219.         return ki.fbStatus & KBDTRF_FINAL_CHAR_IN;
  220.     }
  221. }
  222.  
  223. int vm_getch(void)
  224. {
  225.     if (_osmode == DOS_MODE)
  226.     {
  227.         unsigned char chChar, chScan;
  228.  
  229.         while (!vm_kbhit())
  230.         {
  231.             /* nada */
  232.         }
  233.  
  234.         vm_getkey(&chScan, &chChar);
  235.         if (chChar == 0xe0)
  236.         {
  237.             if (chScan)
  238.             {
  239.                 chChar = 0;     /* force scan return */
  240.             }
  241.             else
  242.             {                   /* get next block */
  243.                 chChar = 0;
  244.  
  245.                 vm_getkey(&chScan, &chChar);
  246.                 if (!chScan)
  247.                 {               /* still no scan? */
  248.                     chScan = chChar;  /* move new char over */
  249.                     chChar = 0; /* force its return */
  250.                 }
  251.                 else
  252.                 {
  253.                     chChar = 0; /* force new scan */
  254.                 }
  255.             }
  256.         }
  257.         if (chScan == 0xe0)
  258.         {
  259.             if (!chChar)
  260.             {
  261.                 chScan = 0;
  262.  
  263.                 vm_getkey(&chScan, &chChar);
  264.                 if (!chScan)
  265.                 {               /* still no scan? */
  266.                     chScan = chChar;  /* move new char over */
  267.                     chChar = 0; /* force its return */
  268.                 }
  269.                 else
  270.                 {
  271.                     chChar = 0; /* force new scan */
  272.                 }
  273.             }
  274.             else
  275.             {
  276.                 chScan = 0;     /* handle 0xe00d case */
  277.             }
  278.         }
  279.         if (chChar)
  280.         {
  281.             chScan = 0;
  282.         }
  283.  
  284.         return (int)((chScan << 8) + (chChar));
  285.     }
  286.     else
  287.     {
  288.         KBDKEYINFO ki;
  289.  
  290.         ki.chChar = 0;
  291.         ki.chScan = 0;
  292.         KbdCharIn(&ki, IO_WAIT, 0);
  293.  
  294.         if (ki.chChar == 0xe0)
  295.         {
  296.             if (ki.chScan)
  297.             {
  298.                 ki.chChar = 0;  /* force scan return */
  299.             }
  300.             else
  301.             {                   /* get next block */
  302.                 ki.chChar = 0;
  303.                 KbdCharIn(&ki, IO_WAIT, 0);
  304.                 if (!ki.chScan)
  305.                 {               /* still no scan? */
  306.                     ki.chScan = ki.chChar;  /* move new char over */
  307.                     ki.chChar = 0;  /* force its return */
  308.                 }
  309.                 else
  310.                 {
  311.                     ki.chChar = 0;  /* force new scan */
  312.                 }
  313.             }
  314.         }
  315.         if (ki.chScan == 0xe0)
  316.         {
  317.             if (!ki.chChar)
  318.             {
  319.                 ki.chScan = 0;
  320.                 KbdCharIn(&ki, IO_WAIT, 0);
  321.                 if (!ki.chScan)
  322.                 {               /* still no scan? */
  323.                     ki.chScan = ki.chChar;  /* move new char over */
  324.                     ki.chChar = 0;  /* force its return */
  325.                 }
  326.                 else
  327.                 {
  328.                     ki.chChar = 0;  /* force new scan */
  329.                 }
  330.             }
  331.             else
  332.             {
  333.                 ki.chScan = 0;  /* handle 0xe00d case */
  334.             }
  335.         }
  336.         if (ki.chChar)
  337.         {
  338.             ki.chScan = 0;
  339.         }
  340.  
  341.         return (int)((ki.chScan << 8) + (ki.chChar));
  342.     }
  343. }
  344.  
  345. char vm_getchxy(char x, char y)
  346. {
  347.     if (_osmode == DOS_MODE)
  348.     {
  349.         char cell[2];
  350.         vi_init();
  351.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  352.         return *cell;
  353.     }
  354.     else
  355.     {
  356.         char cell[2];
  357.         USHORT len = sizeof cell;
  358.         VioReadCharStr(cell, &len, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  359.         return *cell;
  360.     }
  361. }
  362.  
  363. char vm_getattrxy(char x, char y)
  364. {
  365.     if (_osmode == DOS_MODE)
  366.     {
  367.         char cell[2];
  368.         vi_init();
  369.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  370.         return *(cell + 1);
  371.     }
  372.     else
  373.     {
  374.         char cell[2];
  375.         USHORT len = sizeof cell;
  376.         VioReadCellStr(cell, &len, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  377.         return *(cell + 1);
  378.     }
  379. }
  380.  
  381. void vm_xgetchxy(char x, char y, char *attr, char *ch)
  382. {
  383.     if (_osmode == DOS_MODE)
  384.     {
  385.         char cell[2];
  386.         vi_init();
  387.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  388.         *ch = *cell;
  389.         *attr = *(cell + 1);
  390.     }
  391.     else
  392.     {
  393.         char cell[2];
  394.         USHORT len = sizeof cell;
  395.         VioReadCellStr(cell, &len, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  396.         *ch = *cell;
  397.         *attr = *(cell + 1);
  398.     }
  399. }
  400.  
  401. void vm_setcursorstyle(int style)
  402. {
  403.     if (_osmode == DOS_MODE)
  404.     {
  405.         if (vm_iscolorcard())
  406.         {
  407.             switch (style)
  408.             {
  409.             case CURSORHALF:
  410.                 vm_setcursorsize(4, 7);
  411.                 break;
  412.             case CURSORFULL:
  413.                 vm_setcursorsize(0, 7);
  414.                 break;
  415.             case CURSORNORM:
  416.                 vm_setcursorsize(7, 8);
  417.                 break;
  418.             case CURSORHIDE:
  419.                 v_hidecursor();
  420.                 break;
  421.             default:
  422.                 break;
  423.             }
  424.         }
  425.         else
  426.         {
  427.             switch (style)
  428.             {
  429.             case CURSORHALF:
  430.                 vm_setcursorsize(8, 13);
  431.                 break;
  432.             case CURSORFULL:
  433.                 vm_setcursorsize(0, 13);
  434.                 break;
  435.             case CURSORNORM:
  436.                 vm_setcursorsize(11, 13);
  437.                 break;
  438.             case CURSORHIDE:
  439.                 vm_setcursorsize(32, 32);
  440.                 break;
  441.             default:
  442.                 break;
  443.             }
  444.         }
  445.     }
  446.     else
  447.     {
  448.         VIOCURSORINFO vi;
  449.  
  450.         switch (style)
  451.         {
  452.         case CURSORHALF:
  453.             vi.yStart = -50;
  454.             vi.cEnd = -100;
  455.             vi.cx = 0;
  456.             vi.attr = 0;
  457.             VioSetCurType(&vi, 0);
  458.             break;
  459.         case CURSORFULL:
  460.             vi.yStart = 0;
  461.             vi.cEnd = -100;
  462.             vi.cx = 0;
  463.             vi.attr = 0;
  464.             VioSetCurType(&vi, 0);
  465.             break;
  466.         case CURSORNORM:
  467.             vi.yStart = -90;
  468.             vi.cEnd = -100;
  469.             vi.cx = 0;
  470.             vi.attr = 0;
  471.             VioSetCurType(&vi, 0);
  472.             break;
  473.         case CURSORHIDE:
  474.             vi.yStart = -90;
  475.             vi.cEnd = -100;
  476.             vi.cx = 0;
  477.             vi.attr = -1;
  478.             VioSetCurType(&vi, 0);
  479.             break;
  480.         default:
  481.             break;
  482.         }
  483.     }
  484. }
  485.  
  486. void vm_putch(char x, char y, char ch)
  487. {
  488.     if (_osmode == DOS_MODE)
  489.     {
  490.         char cell[2];
  491.         vi_init();
  492.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  493.         *cell = ch;
  494.         v_putline(cell, (int)(x - 1), (int)(y - 1), 1);
  495.     }
  496.     else
  497.     {
  498.         VioWrtCharStr(&ch, 1, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  499.     }
  500. }
  501.  
  502. void vm_puts(char x, char y, char *str)
  503. {
  504.     if (_osmode == DOS_MODE)
  505.     {
  506.         vi_init();
  507.         while (*str)
  508.         {
  509.             vm_putch(x, y, *str);
  510.             str++;
  511.             x++;
  512.         }
  513.     }
  514.     else
  515.     {
  516.         VioWrtCharStr(str, (USHORT) strlen(str), (USHORT) (y - 1), (USHORT) (x - 1), 0);
  517.     }
  518. }
  519.  
  520. void vm_xputch(char x, char y, char attr, char ch)
  521. {
  522.     if (_osmode == DOS_MODE)
  523.     {
  524.         char cell[2];
  525.         vi_init();
  526.         *cell = ch;
  527.         *(cell + 1) = attr;
  528.         v_putline(cell, (int)(x - 1), (int)(y - 1), 1);
  529.     }
  530.     else
  531.     {
  532.         VioWrtCharStrAtt(&ch, 1, (USHORT) (y - 1), (USHORT) (x - 1), (PBYTE) &attr, 0);
  533.     }
  534. }
  535.  
  536. void vm_xputs(char x, char y, char attr, char *str)
  537. {
  538.     if (_osmode == DOS_MODE)
  539.     {
  540.         char *p, *cell, *pcell;
  541.         cell = malloc(strlen(str) * 2);
  542.         if (cell)
  543.         {
  544.             pcell = cell;
  545.             p = str;
  546.             while (*p)
  547.             {
  548.                 *pcell++ = *p++;
  549.                 *pcell++ = attr;
  550.             }
  551.             vi_init();
  552.             v_putline(cell, (int)(x - 1), (int)(y - 1), strlen(str));
  553.             free(cell);
  554.         }
  555.     }
  556.     else
  557.     {
  558.         VioWrtCharStrAtt(str, (USHORT) strlen(str), (USHORT) (y - 1), (USHORT) (x - 1), (PBYTE) &attr, 0);
  559.     }
  560. }
  561.  
  562. void vm_putattr(char x, char y, char attr)
  563. {
  564.     if (_osmode == DOS_MODE)
  565.     {
  566.         char cell[2];
  567.         vi_init();
  568.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  569.         *(cell + 1) = attr;
  570.         v_putline(cell, (int)(x - 1), (int)(y - 1), 1);
  571.     }
  572.     else
  573.     {
  574.         VioWrtNAttr((PBYTE) &attr, 1, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  575.     }
  576. }
  577.  
  578. void vm_paintclearbox(char x1, char y1, char x2, char y2, char attr)
  579. {
  580.     if (_osmode == DOS_MODE)
  581.     {
  582.         char x, y;
  583.         for (y = y1; y <= y2; y++)
  584.         {
  585.             for (x = x1; x <= x2; x++)
  586.             {
  587.                 vm_xputch(x, y, attr, ' ');
  588.             }
  589.         }
  590.     }
  591.     else
  592.     {
  593.         char y, cell[2];
  594.         cell[0] = ' ';
  595.         cell[1] = attr;
  596.         for (y = y1; y <= y2; y++)
  597.         {
  598.             VioWrtNCell((PBYTE) &cell, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  599.         }
  600.     }
  601. }
  602.  
  603. void vm_paintbox(char x1, char y1, char x2, char y2, char attr)
  604. {
  605.     if (_osmode == DOS_MODE)
  606.     {
  607.         char x, y;
  608.         for (y = y1; y <= y2; y++)
  609.         {
  610.             for (x = x1; x <= x2; x++)
  611.             {
  612.                 vm_putattr(x, y, attr);
  613.             }
  614.         }
  615.     }
  616.     else
  617.     {
  618.         char y;
  619.         for (y = y1; y <= y2; y++)
  620.         {
  621.             VioWrtNAttr((PBYTE) &attr, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  622.         }
  623.     }
  624. }
  625.  
  626. void vm_clearbox(char x1, char y1, char x2, char y2)
  627. {
  628.     if (_osmode == DOS_MODE)
  629.     {
  630.         char x, y;
  631.         for (y = y1; y <= y2; y++)
  632.         {
  633.             for (x = x1; x <= x2; x++)
  634.             {
  635.                 vm_putch(x, y, ' ');
  636.             }
  637.         }
  638.     }
  639.     else
  640.     {
  641.         char y, ch;
  642.         ch = ' ';
  643.         for (y = y1; y <= y2; y++)
  644.         {
  645.             VioWrtNChar((PBYTE) &ch, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  646.         }
  647.     }
  648. }
  649.  
  650. void vm_fillbox(char x1, char y1, char x2, char y2, char ch)
  651. {
  652.     if (_osmode == DOS_MODE)
  653.     {
  654.         char x, y;
  655.         for (y = y1; y <= y2; y++)
  656.         {
  657.             for (x = x1; x <= x2; x++)
  658.             {
  659.                 vm_putch(x, y, ch);
  660.             }
  661.         }
  662.     }
  663.     else
  664.     {
  665.         char y;
  666.         for (y = y1; y <= y2; y++)
  667.         {
  668.             VioWrtNChar((PBYTE) &ch, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  669.         }
  670.     }
  671. }
  672.  
  673. void vm_gettext(char x1, char y1, char x2, char y2, char *dest)
  674. {
  675.     if (_osmode == DOS_MODE)
  676.     {
  677.         char x, y;
  678.         for (y = y1; y <= y2; y++)
  679.         {
  680.             for (x = x1; x <= x2; x++)
  681.             {
  682.                 vm_xgetchxy(x, y, dest + 1, dest);
  683.                 dest += 2;
  684.             }
  685.         }
  686.     }
  687.     else
  688.     {
  689.         USHORT width;
  690.         char y;
  691.         width = (USHORT) ((x2 - x1 + 1) * 2);
  692.         for (y = y1; y <= y2; y++)
  693.         {
  694.             VioReadCellStr((PBYTE) dest, &width, (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  695.             dest += width;
  696.         }
  697.     }
  698. }
  699.  
  700. void vm_puttext(char x1, char y1, char x2, char y2, char *srce)
  701. {
  702.     if (_osmode == DOS_MODE)
  703.     {
  704.         char x, y;
  705.         for (y = y1; y <= y2; y++)
  706.         {
  707.             for (x = x1; x <= x2; x++)
  708.             {
  709.                 vm_xputch(x, y, *(srce + 1), *srce);
  710.                 srce += 2;
  711.             }
  712.         }
  713.     }
  714.     else
  715.     {
  716.         USHORT width;
  717.         char y;
  718.         width = (USHORT) ((x2 - x1 + 1) * 2);
  719.         for (y = y1; y <= y2; y++)
  720.         {
  721.             VioWrtCellStr((PBYTE) srce, width, (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  722.             srce += width;
  723.         }
  724.     }
  725. }
  726.  
  727. void vm_horizline(char x1, char x2, char row, char attr, char ch)
  728. {
  729.     if (_osmode == DOS_MODE)
  730.     {
  731.         char x;
  732.         for (x = x1; x <= x2; x++)
  733.         {
  734.             vm_xputch(x, row, attr, ch);
  735.         }
  736.     }
  737.     else
  738.     {
  739.         char cell[2];
  740.         cell[0] = ch;
  741.         cell[1] = attr;
  742.         VioWrtNCell((PBYTE) &cell, (USHORT) (x2 - x1 + 1), (USHORT) (row - 1), (USHORT) (x1 - 1), 0);
  743.     }
  744. }
  745.